package com.admin.boot.common.config;

import cn.dev33.satoken.stp.StpUtil;
import com.admin.boot.modules.system.entity.SysUser;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @version v3.0
 * @Description: TODO
 * @author: jeecg-boot
 * @date: 2024-04-02
 */
public class FieldAutoFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        SysUser loginUser = (SysUser) StpUtil.getSession().get("user");
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        if (loginUser!=null){
            this.strictInsertFill(metaObject,"createBy",String.class,loginUser.getId());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        SysUser loginUser = (SysUser) StpUtil.getSession().get("user");
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        if (loginUser!=null){
            this.strictInsertFill(metaObject,"updateBy",String.class,loginUser.getId());
        }
    }
}
